package com.xc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xc.common.ServerResponse;
import com.xc.dao.SiteArticleMapper;
import com.xc.pojo.SiteArticle;
import com.xc.service.ISiteArticleService;
import org.apache.catalina.Server;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author mipha
 * @version 1.0
 * @date 2019/2/16 2:54 PM
 */
@Service("iSiteArticleService")
public class SiteArticleServiceImpl implements ISiteArticleService {
    @Autowired
    SiteArticleMapper siteArticleMapper;


    @Override
    public ServerResponse<PageInfo> listByAdmin(String artTitle, String artType, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<SiteArticle> siteArticles = siteArticleMapper.listByAdmin(artTitle,artType);
        PageInfo pageInfo = new PageInfo(siteArticles);
        return ServerResponse.createBySuccess(pageInfo);
    }

    @Override
    public ServerResponse add(SiteArticle siteArticle) {
        if(StringUtils.isBlank(siteArticle.getArtTitle())
                || StringUtils.isBlank(siteArticle.getArtType())
                || StringUtils.isBlank(siteArticle.getArtCnt())
                || siteArticle.getIsShow() == null
                ){
            return ServerResponse.createByErrorMsg("标题正文类型必填");
        }

        siteArticle.setAddTime(new Date());

        int insertCount = siteArticleMapper.insert(siteArticle);
        if(insertCount > 0){
            return ServerResponse.createBySuccessMsg("添加成功");
        }else{
            return ServerResponse.createByErrorMsg("添加失败");
        }
    }

    @Override
    public ServerResponse update(SiteArticle siteArticle) {
        if(siteArticle.getId() == null){
            return ServerResponse.createByErrorMsg("修改id必传");
        }
        int updateCount = siteArticleMapper.updateByPrimaryKeySelective(siteArticle);
        if(updateCount > 0){
            return ServerResponse.createBySuccessMsg("修改成功");
        }else{
            return ServerResponse.createByErrorMsg("修改失败");
        }
    }

    @Override
    public ServerResponse detail(Integer artId) {
        return ServerResponse.createBySuccess(siteArticleMapper.selectByPrimaryKey(artId));
    }

    @Override
    public ServerResponse list(String artTitle, String artType, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<SiteArticle> siteArticles = siteArticleMapper.list(artTitle,artType);
        PageInfo pageInfo = new PageInfo(siteArticles);
        return ServerResponse.createBySuccess(pageInfo);
    }


}
